package me.siphone.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import org.linphone.R;

public class CallFragment extends BaseFragment {

    private ImageView ivBack;

    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_call, container, false);
        ivBack = view.findViewById(R.id.iv_back);
        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.fl_call_container, new CallTypeFragment())
                .addToBackStack(CallTypeFragment.class.getSimpleName())
                .commit();
        getChildFragmentManager()
                .addOnBackStackChangedListener(
                        new FragmentManager.OnBackStackChangedListener() {
                            @Override
                            public void onBackStackChanged() {
                                int count = getChildFragmentManager().getBackStackEntryCount();
                                if (count > 1) {
                                    ivBack.setVisibility(View.VISIBLE);
                                } else {
                                    ivBack.setVisibility(View.GONE);
                                }
                            }
                        });
        ivBack.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        getChildFragmentManager().popBackStack();
                    }
                });
        return view;
    }
}
